#include "TextScrollBar.h"
#include "SurfaceFactory.h"

TextScrollBar::TextScrollBar(int posX, int posY, Widget *parent,
		std::list<std::string> textElements,SDL_Surface* surface) :
	Widget(posX, posY, parent) {

	background = surface;

	parcel = getPosition();
	parcel.height = background->h;
	parcel.width = background->w;

	upButton = new Button("", 500, 0, this,
			new TextUpButtonListener(&this->scrollBarElements),SurfaceFactory::getSurface(SURFACEBUTTONSCROLLBARUP_RELEASED),SurfaceFactory::getSurface(SURFACEBUTTONSCROLLBARUP_PRESSED));
	downButton = new Button("", 500, 200, this,
			new TextDownButtonListener(&this->scrollBarElements),SurfaceFactory::getSurface(SURFACEBUTTONSCROLLBARDOWN_RELEASED),SurfaceFactory::getSurface(SURFACEBUTTONSCROLLBARDOWN_PRESSED));

	createTextScrollBarElements(textElements);
	focused = NULL;

}

void TextScrollBar::handleEvent(Event event) {

	this->downButton->handleEvent(event);
	this->upButton->handleEvent(event);

	TextScrollBarElement* aux = NULL;
	for (this->it_element = this->scrollBarElements.begin(); this->it_element
			!= this->scrollBarElements.end(); ++this->it_element) {

		aux = (TextScrollBarElement*) *it_element;
		if (aux->isFocused()) {
			aux->setFocus();
			focused = aux;
		}
		aux->handleEvent(event);
		if ((focused != NULL) && (focused != aux) && (aux->isFocused())) {
			focused->unsetFocus();
			focused = aux;
		}
	}
}

void TextScrollBar::resolve() {
	this->downButton->resolve();
	this->upButton->resolve();
}

void TextScrollBar::showOn(SDL_Surface *surface) {

	SDL_Rect rect;
	rect.x = parcel.posX;
	rect.y = parcel.posY;
	rect.w = parcel.width;
	rect.h = parcel.height;
	SDL_BlitSurface(background, NULL, surface, &rect);

	TextScrollBarElement* aux = NULL;
	for (it_element = scrollBarElements.begin(); it_element
			!= scrollBarElements.end(); ++it_element) {
		aux = (TextScrollBarElement*) *it_element;
		aux->showOn(surface);
	}

	upButton->showOn(surface);
	downButton->showOn(surface);
}

TextScrollBar::~TextScrollBar() {
	// TODO completar
}

void TextScrollBar::appendElement(std::string name) {
	//Verifico que no exista un elemento con la misma descripcion
	TextScrollBarElement* elementoViejo = NULL;

	for (it_element = scrollBarElements.begin(); it_element
			!= scrollBarElements.end(); ++it_element) {
		elementoViejo = (TextScrollBarElement*) *it_element;
		if (elementoViejo->getText().compare(name.c_str()) == 0) {
			//			Logger::log("TextScrollBar", "appendElement",
			//					"No se agrego el elemento, ya existe uno con la misma descripcion");
			//TODO mostrar mensaje de error
			return;
		}
	}
	TextScrollBarElement* scrollBarText = new TextScrollBarElement(0,
			this->scrollBarElements.size() * 20, this, name, parcel.posY,SurfaceFactory::getSurface(SURFACETEXTBOXSCROLLBARELEMENT_RELEASED),SurfaceFactory::getSurface(SURFACETEXTBOXSCROLLBARELEMENT_PRESSED));

	this->scrollBarElements.push_back(scrollBarText);
	this->updatePositions();
}

bool TextScrollBar::deleteSelectedElement() {
	if (!(this->focused == NULL)) {
		std::string text = this->focused->getText();
		if (this->deleteElement(text)) {
			Persistor::deleteScenario(text);
			return true;
		}
	}
	return false;
}

std::string TextScrollBar::getFocusedText() {
	if (this->focused != NULL) {
		return this->focused->getText();
	} else {
		//		Logger::log("TextScrollBar","getFocusedText","focused element is null");
		return "";
	}
}

/**Métodos privados**/
void TextScrollBar::createTextScrollBarElements(
		std::list<std::string> scenarios) {
	std::list<std::string>::iterator it;
	int n = 0;
	TextScrollBarElement* scrollBarText = NULL;
	//stringstream ss;
	for (it = scenarios.begin(); it != scenarios.end(); it++) {
		scrollBarText
				= new TextScrollBarElement(0, n, this, (*it), parcel.posY,SurfaceFactory::getSurface(SURFACETEXTBOXSCROLLBARELEMENT_RELEASED),SurfaceFactory::getSurface(SURFACETEXTBOXSCROLLBARELEMENT_PRESSED));
		this->scrollBarElements.push_back(scrollBarText);
		//ss <<"Escenario: "<<*it<<", Posicion: "<<n ;
		n += 20;
		//Logger::log("TextScrollBar", "createTextScrollBarElements", &ss );
	}
}

void TextScrollBar::updatePositions() {
	int n = 0;
	int aux = 0;
	for (it_element = scrollBarElements.begin(); it_element
			!= scrollBarElements.end(); it_element++) {
		(*it_element)->setYpos(aux);
		n++;
		aux = n * 20;
	}
}

bool TextScrollBar::deleteElement(std::string name) {
	bool found = false;
	std::list<TextScrollBarElement*>::iterator aux;

	for (it_element = scrollBarElements.begin(); it_element
			!= scrollBarElements.end(); it_element++) {

		if ((*it_element)->getText() == name) {
			aux = (it_element);
			found = true;
			Logger::log("TextScrollBar", "deleteElement", "elemento a borrar encontrado");
		}
	}
	if (found) {
		scrollBarElements.erase(aux);
		updatePositions();
	}
	std::stringstream ss;
	ss << found;
	Logger::log("TextScrollBar", "deleteElement", &ss);
	return found;
}
